package com.vibration.baseinfo.service.impl;

import java.net.Socket;
import java.util.*;

import com.vibration.baseinfo.domain.CzTemperatureCorrect;
import com.vibration.baseinfo.domain.CzTemperatureData;
import com.vibration.baseinfo.mapper.CzTemperatureCorrectMapper;
import com.vibration.baseinfo.service.ICzTemperatureCorrectService;
import com.vibration.common.annotation.DataScope;
import com.vibration.socket.VibrationSocketServer;
import com.vibration.socket.domain.DataCollection;
import com.vibration.socket.util.SocketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vibration.baseinfo.mapper.CzEquipmentMapper;
import com.vibration.baseinfo.domain.CzEquipment;
import com.vibration.baseinfo.service.ICzEquipmentService;

/**
 * 设备管理Service业务层处理
 * 
 * @author wu
 * @date 2022-08-11
 */
@Service
public class CzEquipmentServiceImpl implements ICzEquipmentService 
{
    @Autowired
    private CzEquipmentMapper czEquipmentMapper;
    @Autowired
    private CzTemperatureCorrectMapper czTemperatureCorrectMapper;
    @Autowired
    private ICzTemperatureCorrectService temperatureCorrectService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public CzEquipment selectCzEquipmentById(Long id)
    {
        return czEquipmentMapper.selectCzEquipmentById(id);
    }

    @Override
    public CzEquipment selectCzEquipmentByEquipmentCode(String EquipmentCode) {
        List<CzEquipment> equipmentList = czEquipmentMapper.selectCzEquipmentByEquipmentCode(EquipmentCode);
        if(equipmentList.size()>0)return equipmentList.get(0);
        return null;
    }

    /**
     * 查询设备管理列表
     * 
     * @param czEquipment 设备管理
     * @return 设备管理
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<CzEquipment> selectCzEquipmentList(CzEquipment czEquipment)
    {
        return czEquipmentMapper.selectCzEquipmentList(czEquipment);
    }

    /**
     * 新增设备管理
     * 
     * @param czEquipment 设备管理
     * @return 结果
     */
    @Override
    public int insertCzEquipment(CzEquipment czEquipment)
    {
        int c = czEquipmentMapper.insertCzEquipment(czEquipment);
        //更新设备列表
        selectAllEquipment();
        //如果是多点测温设备，增加测温矫正系数
        if("2".equals(czEquipment.getEquipmentType())){
            CzTemperatureCorrect tc = new CzTemperatureCorrect();
            tc.setEquipmentCode(czEquipment.getGyroscopeCode());
            tc.setCorrect5(0.00);
            tc.setCorrect10(0.00);
            tc.setCorrect15(0.00);
            tc.setCorrect20(0.00);
            tc.setCorrect25(0.00);
            tc.setCorrect30(0.00);
            tc.setCorrect35(0.00);
            tc.setCorrect40(0.00);
            tc.setCorrect45(0.00);
            tc.setCorrect50(0.00);
            czTemperatureCorrectMapper.insertCzTemperatureCorrect(tc);
            temperatureCorrectService.checkTemperatureCorrect();
        }
        return c;
    }

    /**
     * 修改设备管理
     * 
     * @param czEquipment 设备管理
     * @return 结果
     */
    @Override
    public int updateCzEquipment(CzEquipment czEquipment)
    {
        int c = czEquipmentMapper.updateCzEquipment(czEquipment);
        //更新设备列表
        selectAllEquipment();
        return c;
    }

    /**
     * 修改设备管理 修改设备主动上传数据 不更新设备列表
     *
     * @param czEquipment 设备管理
     * @return 结果
     */
    @Override
    public int updateEquipment(CzEquipment czEquipment)
    {

        return czEquipmentMapper.updateCzEquipment(czEquipment);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteCzEquipmentByIds(Long[] ids)
    {
        int c = czEquipmentMapper.deleteCzEquipmentByIds(ids);
        //更新设备列表
        selectAllEquipment();
        return c;
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteCzEquipmentById(Long id)
    {
        int c = czEquipmentMapper.deleteCzEquipmentById(id);
        //更新设备列表
        selectAllEquipment();
        return c;
    }

    @Override
    public void selectAllEquipment() {
        List<CzEquipment> equipmentList = czEquipmentMapper.selectAllCzEquipmentList();
        SocketUtil.equipmentMap =new HashMap<>();
        SocketUtil.gyroscopeDataMap = new HashMap<>();
        for (CzEquipment czEquipment : equipmentList) {
            SocketUtil.equipmentMap.put(czEquipment.getSn(),czEquipment);
            SocketUtil.gyroscopeDataMap.put(czEquipment.getGyroscopeCode(),czEquipment);
        }
    }

    @Override
    public List<CzEquipment> selectEquipmentListAndData() {
        List<CzEquipment> equipmentList = selectCzEquipmentList(new CzEquipment());
        //获取实时设备数据
        for (CzEquipment equipment : equipmentList) {
            CzEquipment e = SocketUtil.equipmentMap.get(equipment.getSn());
            if(e!=null){
                equipment.setDataList(e.getDataList());
            }
            DataCollection dataCollection = SocketUtil.gyroscopeSocketMap.get(equipment.getGyroscopeCode());
            if(dataCollection!=null){
                equipment.setGyroscopeData(dataCollection.getGyroscopeData());
            }
        }
        return equipmentList;
    }

    //获取设备列表及任务状态
    @Override
    public List<CzEquipment> selectEquipmentListAndStatus() {
        List<CzEquipment> equipmentList = selectCzEquipmentList(new CzEquipment());
        //判断设备在线状态
        for (CzEquipment equipment : equipmentList) {
            if("1".equals(equipment.getEquipmentType())){//台站
                CzEquipment e = SocketUtil.equipmentMap.get(equipment.getSn());
                if(e==null || e.getDataList() == null){
                    equipment.setOnLineStatus(1);
                }else {
                    //计算时间差
                    long cha = new Date().getTime() - e.getDataList().getDataTime().getTime();
                    if(cha < 3*60*1000){
                        equipment.setOnLineStatus(0);
                    }else {
                        equipment.setOnLineStatus(1);
                    }
                }
            }else if("2".equals(equipment.getEquipmentType())){

                DataCollection dc =SocketUtil.gyroscopeSocketMap.get(equipment.getGyroscopeCode());
                if(dc == null || dc.getMoreTemperatureData() == null){
                    equipment.setOnLineStatus(1);
                }else {
                    long cha = new Date().getTime() - dc.getMoreTemperatureData().getDataTime().getTime();
                    if(cha < 3*60*1000){
                        equipment.setOnLineStatus(0);
                    }else {
                        equipment.setOnLineStatus(1);
                    }
                }
            }
        }

        return equipmentList;
    }

    @Override
    public CzEquipment selectEquipmentInfo(String equipmentSn) {
        CzEquipment equipment = czEquipmentMapper.selectCzEquipmentBySn(equipmentSn);
        CzEquipment e = SocketUtil.equipmentMap.get(equipment.getSn());
        if(e!=null){
            equipment.setDataList(e.getDataList());
        }
        DataCollection dataCollection = SocketUtil.gyroscopeSocketMap.get(equipment.getGyroscopeCode());
        if(dataCollection!=null){
            //equipment.setGyroscopeData(dataCollection.getGyroscopeData());
            CzTemperatureData temData = dataCollection.getTemperatureData();
            equipment.setTemperatureData(temData);
        }
        return equipment;
    }

    @Override
    public List<CzEquipment> selectAllEquipment1() {
        return czEquipmentMapper.selectAllCzEquipmentList();
    }


}
